package handlers;

import actions.PlatarBomba;

import com.jme.input.InputHandler;
import com.jme.input.KeyBindingManager;
import com.jme.input.KeyInput;
import com.jme.input.action.KeyNodeBackwardAction;
import com.jme.input.action.KeyNodeForwardAction;
import com.jme.input.action.KeyNodeRotateLeftAction;
import com.jme.input.action.KeyNodeRotateRightAction;
import com.jme.scene.Node;
import com.jmex.physics.DynamicPhysicsNode;
import com.jmex.physics.PhysicsSpace;
import com.model.md5.controller.MD5Controller;

/**
 * Input Handler for the Flag Rush game. This controls a supplied spatial
 * allowing us to move it forward, backward and rotate it left and right.
 * 
 * @author Mark Powell
 * 
 */
public class ManejadorDeTeclas extends InputHandler {

	/**
	 * Supply the node to control and the api that will handle input creation.
	 * 
	 * @param node
	 *            the node we wish to move
	 * @param mazeData
	 * @param rootNode
	 * @param physicsSpace
	 * @param bodyController 
	 * @param bodyController 
	 * @param animController 
	 * @param movimientos 
	 * @param updater 
	 */
	public ManejadorDeTeclas(DynamicPhysicsNode node, boolean[][] mazeData,
			Node rootNode, PhysicsSpace physicsSpace, MD5Controller bodyController) {

		setKeyBindings();
		setActions(node, rootNode, physicsSpace, mazeData,bodyController);

	}

	/**
	 * assigns action classes to triggers. These actions handle moving the node
	 * forward, backward and rotating it.
	 * 
	 * @param node
	 *            the node to control.
	 * @param rootNode
	 * @param physicsSpace
	 * @param posz
	 * @param posx
	 * @param scene
	 * @param mazeData
	 * @param bodyController 
	 * @param movimientos 
	 * @param animController 
	 * @param updater 
	 */
	private void setActions(DynamicPhysicsNode node, Node rootNode,
			PhysicsSpace physicsSpace, boolean[][] mazeData, MD5Controller bodyController) {

		PlatarBomba plantar = new PlatarBomba(node, rootNode, physicsSpace,mazeData,bodyController);
		addAction(plantar, "plantar", false);

		KeyNodeForwardAction forward = new KeyNodeForwardAction(node, 30f);
		addAction(forward, "forward", true);

		KeyNodeBackwardAction backward = new KeyNodeBackwardAction(node, 15f);
		addAction(backward, "backward", true);

		KeyNodeRotateRightAction rotateRight = new KeyNodeRotateRightAction(
				node, 5f);
		rotateRight.setLockAxis(node.getLocalRotation().getRotationColumn(1));
		addAction(rotateRight, "turnRight", true);

		KeyNodeRotateLeftAction rotateLeft = new KeyNodeRotateLeftAction(node,
				5f);
		rotateLeft.setLockAxis(node.getLocalRotation().getRotationColumn(1));
		addAction(rotateLeft, "turnLeft", true);
	}

	/**
	 * creates the keyboard object, allowing us to obtain the values of a
	 * keyboard as keys are pressed. It then sets the actions to be triggered
	 * based on if certain keys are pressed (WSAD).
	 * 
	 * @param api
	 */

	private void setKeyBindings() {
		KeyBindingManager keyboard = KeyBindingManager.getKeyBindingManager();

		keyboard.set("forward", KeyInput.KEY_W);
		keyboard.set("backward", KeyInput.KEY_S);
		keyboard.set("turnRight", KeyInput.KEY_D);
		keyboard.set("turnLeft", KeyInput.KEY_A);
		keyboard.set("plantar", KeyInput.KEY_RETURN);

		// removeFromAttachedHandlers();

	}
}
